package com.pdfbox.sample.quickstart;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import org.apache.pdfbox.Loader;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

public class _15ExtractingImage {

  public static void main (String args[]) throws IOException {
    String existFileName = "E:/PdfBox_Examples/周报_add_png.pdf";
    String exportFileName = "E:/PdfBox_Examples/etracting_pic.png";

    // TODO 渲染的表格乱码了
    // 第1步:加载现有PDF文档
    File file = new File(existFileName);
//    PDDocument document = PDDocument.load(file);
    PDDocument document = Loader.loadPDF(file);

    // 第2步:实例化PDFRenderer类
    PDFRenderer renderer = new PDFRenderer(document);

    // 第3步:从PDF文档渲染图像，渲染第 0 页数据
    BufferedImage image = renderer.renderImage(0);
//    BufferedImage image = renderer.renderImage(1);

    // 第4步:将图像写入文件
    ImageIO.write(image, "JPEG", new File(exportFileName));
    System.out.println("Image created");

    // 第5步:关闭文档
    document.close();

    System.out.println("export " + exportFileName + " success");
  }

}
